#include "util.h"
#include "rtc.h"

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <termios.h>

struct termios orig_termios;

void reset_terminal_mode()
{
    tcsetattr(0, TCSANOW, &orig_termios);
}

void set_conio_terminal_mode()
{
    struct termios new_termios;

    /* take two copies - one for now, one for later */
    tcgetattr(0, &orig_termios);
    memcpy(&new_termios, &orig_termios, sizeof(new_termios));

    /* register cleanup handler, and set the new terminal mode */
    atexit(reset_terminal_mode);
    //cfmakeraw(&new_termios);
//    termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    new_termios.c_lflag &= ~(ECHO | ECHONL | ICANON);
//    new_termios.c_lflag &= ~(ICANON);
    tcsetattr(0, TCSANOW, &new_termios);
}

int kbhit()
{
    struct timeval tv = { 0L, 0L };
    fd_set fds;
    FD_ZERO( &fds);
    FD_SET( 0, &fds);
    return select( 1, &fds, NULL, NULL, &tv);
}

int getch()
{
    //printf( "getch: enter\n");

    int r;
    unsigned char c = 0;

    r = read( 0, &c, sizeof(c));
	//printf( "getch: '%c' (%x) res=%d\n", c, c, r);
    if( r < 0) {
    	//printf( "getch: Error %d reading a char\n", r);
        return 0;
    }
    return c;
}





